package service

import (
	"360video/video-srv/handler/config"
	__ "360video/video-srv/handler/proto"
	"360video/video-srv/service/models"
	"context"
	"encoding/json"
	"fmt"
	"time"
)

type Server struct {
	__.UnimplementedVideoServer
}

// SayHello implements helloworld.GreeterServer
func (s *Server) Register(_ context.Context, in *__.RegisterReq) (*__.RegisterResp, error) {
	user := models.User{
		Account:  in.Account,
		Password: in.Password,
	}
	config.Rdb.Get(config.Ctx, "sendSms"+in.Account)
	if models.Register(&user) != nil {
		return nil, fmt.Errorf("注册失败")
	}
	return &__.RegisterResp{
		Id: int64(user.ID),
	}, nil
}
func (s *Server) Login(_ context.Context, in *__.LoginReq) (*__.LoginResp, error) {
	user := models.User{
		Account: in.Account,
	}
	//get := config.Rdb.Get(config.Ctx, "sendSms"+in.Account)
	//if get.Val() != in.SendSms {
	//	return nil, fmt.Errorf("短信错误")
	//}
	if models.Login(&user) != nil {
		return nil, fmt.Errorf("登录失败")
	}
	return &__.LoginResp{
		Id: int64(user.ID),
	}, nil
}
func (s *Server) VideoAdd(_ context.Context, in *__.VideoAddReq) (*__.VideoAddResp, error) {
	video := models.Video{
		UserId:  int(in.UserId),
		Titer:   in.Titer,
		Message: in.Message,
		Static:  in.Static,
		Address: in.Address,
		Year:    in.Year,
		Image:   in.Image,
	}
	if models.VideoAdd(&video) != nil {
		return nil, fmt.Errorf("添加失败")
	}
	return &__.VideoAddResp{
		Id: int64(video.ID),
	}, nil
}
func (s *Server) VideoList(_ context.Context, in *__.VideoListReq) (*__.VideoListResp, error) {
	var video []models.Video
	if models.VideoList(&video, int(in.Size), int(in.Page), in.Address) != nil {
		return nil, fmt.Errorf("查询失败")
	}
	get := config.Rdb.Get(config.Ctx, "videoList")
	if get.Val() == "" {
		config.DB.Find(&models.Video{})
		marshal, err := json.Marshal(video)
		if err != nil {
			return nil, err
		}
		config.Rdb.Set(config.Ctx, "videoList", marshal, time.Minute*10)
	} else {
		json.Unmarshal([]byte(get.Val()), &video)
	}
	var lists []*__.List
	for _, m := range video {
		list := __.List{
			UserId:  int64(m.UserId),
			Titer:   m.Titer,
			Message: m.Message,
			Static:  m.Static,
			Address: m.Address,
			Year:    m.Year,
			Image:   m.Image,
		}
		lists = append(lists, &list)
	}

	return &__.VideoListResp{
		Lists: lists,
	}, nil
}
func (s *Server) VideoDetails(_ context.Context, in *__.VideoDetailsReq) (*__.VideoDetailsResp, error) {
	var video models.Video
	if models.VideoDetails(&video, int(in.Id)) != nil {
		return nil, fmt.Errorf("查询失败")
	}
	return &__.VideoDetailsResp{
		UserId:  int64(video.UserId),
		Titer:   video.Titer,
		Message: video.Message,
		Static:  video.Static,
		Address: video.Address,
		Year:    video.Year,
		Image:   video.Image,
	}, nil
}
func (s *Server) VideosType(_ context.Context, in *__.VideoTypeReq) (*__.VideoTypeResp, error) {
	var video models.Video
	if models.VideoType(&video, in.Static) != nil {
		return nil, fmt.Errorf("查询失败")
	}
	return &__.VideoTypeResp{
		UserId:  int64(video.UserId),
		Titer:   video.Titer,
		Message: video.Message,
		Address: video.Address,
		Year:    video.Year,
		Image:   video.Image,
	}, nil
}
func (s *Server) EpisodeList(_ context.Context, in *__.EpisodeListReq) (*__.EpisodeListResp, error) {
	var episode []models.Episode
	if models.EpisodeList(&episode) != nil {
		return nil, fmt.Errorf("查询失败")
	}
	var lists []*__.List1
	for _, m := range episode {
		list := __.List1{
			VideoId: int64(m.VideoId),
			Title:   m.Title,
			Time:    m.Time,
		}
		lists = append(lists, &list)
	}

	return &__.EpisodeListResp{
		Lists: lists,
	}, nil
}
func (s *Server) HisToryAdd(_ context.Context, in *__.HisToryAddReq) (*__.HisToryAddResp, error) {
	hisTory := models.HisTory{
		UserId:    int(in.UserId),
		VideoId:   int(in.VideoId),
		EpisodeId: int(in.EpisodeId),
		Progress:  int(in.Progress),
	}
	if models.HisToryAdd(&hisTory) != nil {
		return nil, fmt.Errorf("添加失败")
	}
	return &__.HisToryAddResp{
		Id: int64(hisTory.ID),
	}, nil
}
func (s *Server) HisToryList(_ context.Context, in *__.HisToryListReq) (*__.HisToryListResp, error) {
	var hisTory models.HisTory
	if models.HisToryList(&hisTory) != nil {
		return nil, fmt.Errorf("查询失败")
	}
	return &__.HisToryListResp{
		UserId:    int64(hisTory.UserId),
		VideoId:   int64(hisTory.VideoId),
		EpisodeId: int64(hisTory.EpisodeId),
		Progress:  int64(hisTory.Progress),
	}, nil
}
func (s *Server) HotList(_ context.Context, in *__.HotListReq) (*__.HotListResp, error) {
	var hot models.Hot
	if models.HotList(&hot) != nil {
		return nil, fmt.Errorf("查询失败")
	}
	var hotnum = (hot.ViewCount * 0.6) + (hot.LikeCount * 0.2) + (hot.CollCount * 0.1) + (hot.TakeCount * 0.1)
	return &__.HotListResp{
		HotHum: float32(hotnum),
	}, nil
}
